package com.wsj.web.action.homeSiteNews;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import com.wsj.util.tool.DateUtil;

import com.wsj.service.IBmClassTypeServ;
import com.wsj.service.IHomeSiteAdBannerDescriptServ;
import com.wsj.service.IHomeSiteAdBannerServ;
import com.wsj.service.IHomeSiteNewsClassServ;
import com.wsj.service.IHomeSiteNewsServ;
import com.wsj.service.IUserManagerServ;
import com.wsj.util.exception.BusinessException;
import com.wsj.util.page.PageResultSet;
import com.wsj.util.tool.FileUplaodAndManageForStorage;
import com.wsj.util.tool.HtmlCreateObj;
import com.wsj.util.tool.RequestUtils;
import com.wsj.util.tool.StringUtil;
import com.wsj.util.tool.StringUtils;
import com.wsj.web.action.BaseAction;
import com.wsj.web.constant.homeSiteNews.HomeSiteNewsConstant;
import com.wsj.web.constant.page.IndexContent;
import com.wsj.web.constant.page.PageContent;
import com.wsj.web.form.HomeSiteNews.HomeSiteNewsForm;
import com.wsj.web.subclass.displayBean.HomeSiteNews.HomeSiteNewsExtendBean;
import com.wsj.web.subclass.displayBean.HomeSiteNews.HomeSiteNewsFormExtendBean;
import com.wsj.web.subclass.function.HomeSiteNewsCreateHtmlObj;
import com.wsj.web.subclass.function.ProductsParamentObj;
import com.wsj.web.vo.homeSite.BmClassType;
import com.wsj.web.vo.homeSite.HomeSiteAdBanner;
import com.wsj.web.vo.homeSite.HomeSiteAdBannerDescription;
import com.wsj.web.vo.homeSite.HomeSiteNews;
import com.wsj.web.vo.homeSite.HomeSiteNewsClass;
import com.wsj.web.vo.homeSite.UserBase;

public class HomeSiteNewsAction extends BaseAction {
	
	private IBmClassTypeServ bmClassTypeServImpl;
	private FileUplaodAndManageForStorage imgUpload;
	
	//生成html页面对象
	private HtmlCreateObj htmlCreateObj;

	/** 资讯分类接口 */
	private IHomeSiteNewsClassServ homeSiteNewsClassServ;
	
	/** 资讯接口 */
	private IHomeSiteNewsServ homeSiteNewsServImpl;
	
	/** 广告接口 */
	private IHomeSiteAdBannerServ homeSiteAdBannerServ;
	
	/** 广告描述接口 */
	private IHomeSiteAdBannerDescriptServ homeSiteAdBannerDescriptServ;
	
	/******
	 * 用户接口
	 */
	private IUserManagerServ userManagerServ;
	

	public IUserManagerServ getUserManagerServ() {
		return userManagerServ;
	}


	public void setUserManagerServ(IUserManagerServ userManagerServ) {
		this.userManagerServ = userManagerServ;
	}


	/**
	 * 获取新闻点击率 前 50 条新闻
	 * */
	@SuppressWarnings("unchecked")
	public ActionForward top(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception { 
		
	    //  热点新闻   50 
		List<HomeSiteNews> LIST_NEWS_twenty = this.homeSiteNewsServImpl.getTopByHits(50);
		if(StringUtils.isNotNull(LIST_NEWS_twenty)){
			request.setAttribute("HomeSiteNews_list", LIST_NEWS_twenty);
		}
		
		return mapping.findForward("toList");
	}
	
	
	/*****
	 * 资讯静态页面批量生成
	 */
	public ActionForward allToHtml(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception 
	{
		String toPage ="homeSiteNews_createHtmlEnd";
		boolean isok=false;
		Integer page = 1 ;
		Integer pageSize = 100;
		//处理的个数
		Integer dealNum=0;
		HomeSiteNewsFormExtendBean homeSiteNewsFormExtendBean = new HomeSiteNewsFormExtendBean();
		homeSiteNewsFormExtendBean.setPage(page);
		homeSiteNewsFormExtendBean.setPageSize(pageSize);
		
		PageResultSet pageResultSet=this.homeSiteNewsServImpl.loadHomeSiteNewsByExtendBean(homeSiteNewsFormExtendBean);
		if(pageResultSet!=null)
		{
			//获取总页数
			Integer allPage = pageResultSet.getAllPage();
			for(int j=1;j<=allPage;j++)
			{
				HomeSiteNewsFormExtendBean curBean = new HomeSiteNewsFormExtendBean();
				curBean.setPage(j);
				curBean.setPageSize(pageSize);
				
				PageResultSet curPageResultSet=this.homeSiteNewsServImpl.loadHomeSiteNewsByExtendBean(curBean);
				List li = curPageResultSet.getResult();
				for(int t=0;t<li.size();t++)
				{
					try {
						HomeSiteNews homeSiteNews = (HomeSiteNews)li.get(t);
						HomeSiteNewsCreateHtmlObj homeSiteNewsCreateHtmlObj  = new HomeSiteNewsCreateHtmlObj(homeSiteNewsServImpl,htmlCreateObj,request,response,homeSiteNews);
						homeSiteNewsCreateHtmlObj.createFile();
						dealNum++;
						
					} catch (Exception e) 
					{
						e.printStackTrace();
					}
					
				}
				isok=true;
			}
		}
		
		request.setAttribute("createName", "所有资讯");
		request.setAttribute("dealNum", dealNum);
		if(isok)
		{
			this.setPopMsg(request, "msg", "所有资讯已经完全生成!");
		}
		
		
		return mapping.findForward(toPage);
		
	}
	
	/**
	 * 跳转到 静态页生成管理页
	 * */
	public ActionForward to_html_list(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		String topage="to_html_list";
		Integer page = 1 ;
		Integer pageSize = 1000;
		try {
			HomeSiteNewsFormExtendBean homeSiteNewsFormExtendBean = new HomeSiteNewsFormExtendBean();
			if(request.getParameter("page")!=null){
				page = Integer.parseInt(request.getParameter("page"));
				homeSiteNewsFormExtendBean.setPage(page);
			}else{
				homeSiteNewsFormExtendBean.setPage(page);
			}
			homeSiteNewsFormExtendBean.setPageSize(pageSize);
	    	  PageResultSet pageResultSet=this.homeSiteNewsServImpl.loadHomeSiteNewsByExtendBean(homeSiteNewsFormExtendBean);
				  if(pageResultSet!=null)
					{
					  	// 设置上一页
						request.setAttribute(PageContent.backPage,pageResultSet.getBackPage());
						//下一页
						request.setAttribute(PageContent.nextPage,pageResultSet.getNextPage());
						//当前页
						request.setAttribute(PageContent.curPage,pageResultSet.getCurPage());
						
						//总共页数
						request.setAttribute(PageContent.allPage,pageResultSet.getAllPage());
						//大小
						request.setAttribute(PageContent.pageSize,pageResultSet.getPageSize());
						
						request.setAttribute(PageContent.recordNum, pageResultSet.getRecordNum());
						
						
						request.setAttribute("title",null);
						request.setAttribute("content",null);
						request.setAttribute("createDatetimeStr",null);
						request.setAttribute("homeSiteNewsClassid",null);
						request.setAttribute("page",page);
						
						//结果集
						List exresult = pageResultSet.getResult();
						
						request.setAttribute("HomeSiteNewsList", exresult);
						
					}
				  
				  
			} catch (BusinessException e) {
				e.printStackTrace();
			}
		
		return mapping.findForward(topage);
		
	}
	
	
	
	
	
	/**
	 * 通过批量修改，然后生成 html 静态页面
	 * 条件：OID
	 * **/
	public ActionForward update_to_html(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
			String[] idStr = request.getParameterValues("checkboxparam");
			Integer page = 1;
			long[] ids = StringUtil.changeStrArrayToLongArray(idStr);
			
			if(ids != null && ids.length > 0){
	    	  for(int i=0;i<ids.length;i++){
	    		    HomeSiteNews homeSiteNews = this.homeSiteNewsServImpl.getHomeSiteNewsById(ids[i]);
	    		    
	    		    // 生成静态页面
	    			String filePath=HomeSiteNewsConstant.homeSiteNewsHead + homeSiteNews.getId()+".html";
	    			htmlCreateObj.setCreateFileParament(RequestUtils.getRequestHost(request)+HomeSiteNewsConstant.homeSiteNewsHead+homeSiteNews.getId(), "html/news/", filePath);
	    			htmlCreateObj.createHtmlFile(request, response);
	    	        //设置静态文件路径
	    			homeSiteNews.setFilePath(filePath);
	    			
	    			this.homeSiteNewsServImpl.update(homeSiteNews);
	    		  } 
	    	  		request.setAttribute("msg", "<script>alert('资讯：静态页面批量生成成功！！！');</script>");
	    	  }
		
			if(request.getParameter("page")!=null){
				page = Integer.parseInt(request.getParameter("page"));
			}
			
			String topage = "/admin/homeSite/homeSiteNews.do?method=to_html_list&page="+page;
			return new ActionForward(topage);
	}
	
	
	/**
	 * 资讯详细页面
	 * 条件：新闻 OID
	 * **/  
	@SuppressWarnings("unchecked")
	public ActionForward toDetails(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Integer id = new Integer(0);
		HomeSiteNews hsNews = new HomeSiteNews();
		if(StringUtils.isNotNull(request.getParameter("id"))){
			id = Integer.parseInt(request.getParameter("id"));
		}

		
				
		//   新闻详细内容
		if(id != null){
			hsNews = this.homeSiteNewsServImpl.getHomeSiteNewsById(id);
			if(hsNews != null){
				String keyWord = hsNews.getKeyword();				
				//  根据关键字获取 8 条相关新闻
				List<HomeSiteNews> LIST_NEWS_KEY_eight = this.homeSiteNewsServImpl.getTopBykeyword(keyWord, IndexContent.INDEX_TOP_EIGHT);
				if(StringUtils.isNotNull(LIST_NEWS_KEY_eight)){
					request.setAttribute("LIST_NEWS_KEY_eight", LIST_NEWS_KEY_eight);
				}
				
				//获取资讯分类
				HomeSiteNewsClass  homeSiteNewsClass =this.getHomeSiteNewsClassServ().loadNewsClassById(hsNews.getHomeSiteNewsClass().getId());
				//资讯分类
				request.setAttribute("hsNewsClass", homeSiteNewsClass);
				
		    	String spliter="<div style=\"page-break-after: always\"><span style=\"display: none\">&nbsp;</span></div>";
		    	List<String> contentslist=new ArrayList<String>();
				String con=hsNews.getContent();
				if(con!=null&&!con.equals("")&&con.indexOf(spliter)!=-1){
					String[] contents=con.split(spliter);
					if(contents!=null&&contents.length>0){
						for(int i=0;i<contents.length;i++){
							if(contents[i]!=null&&!contents.equals("")){
								contentslist.add(contents[i]);
							}
						}
					}
				}
				//生成多个filePath
				List<String> moreList=new ArrayList<String>();
				String morefilePath="";
				if(contentslist!=null&&!contentslist.isEmpty()&&contentslist.size()>0){
					for(int i=0;i<contentslist.size();i++){
						String link="&nbsp;&nbsp;&nbsp;<a target=_self href=";
						String link1=RequestUtils.getRequestHost(request)+"/html/news/";
						String link2=HomeSiteNewsConstant.homeSiteNewsHead+hsNews.getId()+"_"+String.valueOf(i+1)+".html>";
						String link4=(i+1)+"</a>&nbsp;&nbsp;&nbsp;";
						String contentstr=request.getParameter("content");
							if(contentstr!=null&&!contentstr.equals("")){
								if(Integer.valueOf(contentstr)==i){
									link4="<b><font color=green weight=bold size=2px>"+(i+1)+"</font></b>"+"</a>&nbsp;&nbsp;&nbsp;";
								}
							}
						String url=link+link1+link2+link4;
						morefilePath+=url;
					}	
				}
				if(contentslist!=null&&!contentslist.isEmpty()&&contentslist.size()>0){
					for(int i=0;i<contentslist.size();i++){
						String cont=contentslist.get(i).toString().trim()+"<br/>";
						moreList.add(cont+"<p align=center>"+morefilePath+"</p>");
					}	
				}
				if(moreList!=null&&!moreList.isEmpty()&&moreList.size()>0){
					String content=request.getParameter("content");
					for(int i=0;i<moreList.size();i++){
						if(content!=null&&!content.equals("")){
							if(Integer.valueOf(content)==i){
								hsNews.setContent(moreList.get(i));
							}
							
						}
					}
					
				}	
				//资讯
				request.setAttribute("hsNews", hsNews);
			}
		}
		
		//  顶部广告：列表页衡栏广告
		HomeSiteNewsClass hsClass_mid = this.homeSiteNewsClassServ.loadNewsClassByNames(IndexContent.INDEX_ZPGG_NAME);
		HomeSiteAdBannerDescription hs_Ad_mid_Des = this.homeSiteAdBannerDescriptServ.loadByClassIdAndName(hsClass_mid.getId(), IndexContent.INDEX_NRYHLGG_NAME);
		List<HomeSiteAdBanner> LIST_AD_MID_LIST = this.homeSiteAdBannerServ.loadTopByDesId(hs_Ad_mid_Des.getId(),new Integer(1), IndexContent.INDEX_TOP_ONE);
		if(StringUtils.isNotNull(LIST_AD_MID_LIST)){
			HomeSiteAdBanner hs = (HomeSiteAdBanner)LIST_AD_MID_LIST.get(0);
			request.setAttribute("LIST_AD_MID_LIST", hs);			
		}
		
		//   营商点击排行 6
		List<HomeSiteNews> LIST_NEWS_six = this.homeSiteNewsServImpl.getTopByHits(IndexContent.INDEX_TOP_SIX);
		if(StringUtils.isNotNull(LIST_NEWS_six)){
			request.setAttribute("LIST_NEWS_six", LIST_NEWS_six);
		}
		
		//   营商热点论坛排行 4		
		
		//	   最新活动  8 
		List<HomeSiteNews> LIST_NEWS_ZXHD_eight = this.homeSiteNewsServImpl.getTopByHits(IndexContent.INDEX_TOP_EIGHT);
		if(StringUtils.isNotNull(LIST_NEWS_ZXHD_eight)){
			request.setAttribute("LIST_NEWS_ZXHD_eight", LIST_NEWS_ZXHD_eight);
		}
		
		
		
		return mapping.findForward("toDetails");
	}
	

	
	
	
	
	/**
	 * 去添加
	 * */

	public ActionForward toadd(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		HomeSiteNewsForm homeSiteNewsForm = new HomeSiteNewsForm();
		homeSiteNewsForm.setAudite(new Short("1"));
		homeSiteNewsForm.setIscommend(new Integer("1"));
		homeSiteNewsForm.setToptitle(new Short("1"));
		homeSiteNewsForm.setWriter(HomeSiteNewsConstant.defaultWriter);
		homeSiteNewsForm.setSource(HomeSiteNewsConstant.defaultSource);
		
		//		获取所有的资讯分类的第一级分类
		BmClassType bmClassType=this.bmClassTypeServImpl.getBmClassTypeByClassTypeName(HomeSiteNewsConstant.zxClassTypeName);
		
		//		获取父分类下面的所有一级分类
		this.loadFirstLayerList(request,bmClassType.getId());
		request.setAttribute("bmClassTypeId", bmClassType.getId());
		request.setAttribute("homeSiteNewsForm", homeSiteNewsForm);
		return mapping.findForward("toadd");
		
	}
	
	/*****
	 * 获得所有分类的列表
	 * @param request
	 */
	private void loadFirstLayerList(HttpServletRequest request,Integer typeId) 
	{
		List firstLayerList= null;
		try {
			
			 firstLayerList = this.getHomeSiteNewsClassServ().loadFirstHomeSiteNewsClassByType(typeId);
		} catch (BusinessException e) {
			e.printStackTrace();
		}
		
		if(firstLayerList  == null)
		{
			firstLayerList = new ArrayList();
		}
		
		request.setAttribute("listSiteNewsBClass",firstLayerList);
	}



	
	
	/**
	 * 添加
	 * */
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		HomeSiteNewsForm homeSiteNewsForm=(HomeSiteNewsForm)form;
		HomeSiteNews homeSiteNews=new HomeSiteNews();
		BeanUtils.copyProperties(homeSiteNews, homeSiteNewsForm);
		
		//设置资讯分类
		this.addHomeSiteNewsClass(homeSiteNewsForm, homeSiteNews);
		
		//如果是 股票推荐栏目 ,那么生成xml文件
		this.createCommentStock(request,response,homeSiteNews);

		//上传图片
		if(null!=homeSiteNewsForm.getImgstr()&& !"".equals(homeSiteNewsForm.getImgstr())
				&& null!=homeSiteNewsForm.getImgstr().getFileName()
				&& homeSiteNewsForm.getImgstr().getFileSize()>1024*20){
			//标题 及关键字 内容
			request.setAttribute("title", homeSiteNewsForm.getTitle());
			request.setAttribute("keyword", homeSiteNewsForm.getKeyword());
			request.setAttribute("content", homeSiteNewsForm.getContent());
			request.setAttribute("add", "<script>alert('添加失败！！！，图片不可以大于20k');</script>");
		}
		else{
		
		this.addImg(homeSiteNewsForm, homeSiteNews, request, null);
		//设置 添加时间及修改时间
		java.util.Date date=new java.util.Date();
		homeSiteNews.setCreateDatetime(date);
		homeSiteNews.setUpdateDatetime(date);
		
		
		//获得发布人的信息
		UserBase userBase = this.getUserBaseFromSession(request);
		if(userBase!=null){
			
			long userId=userBase.getUserId();
			if(userId!=0){
				userBase = this.userManagerServ.getUserBaseById(userId);
				homeSiteNews.setUserBase(userBase);
			}
		}
		else{
			//跳转到登陆页面
		}
		this.homeSiteNewsServImpl.add(homeSiteNews);
		
    	String spliter="<div style=\"page-break-after: always\"><span style=\"display: none\">&nbsp;</span></div>";
    	List<String> contentslist=new ArrayList<String>();
		String con=homeSiteNews.getContent();
		if(con!=null&&!con.equals("")&&con.indexOf(spliter)!=-1){
			String[] contents=con.split(spliter);
			if(contents!=null&&contents.length>0){
				for(int i=0;i<contents.length;i++){
					if(contents[i]!=null&&!contents.equals("")){
						contentslist.add(contents[i]);
					}
				}
			}
		}else 
		{
//	        //生成静态页面  +"_"+String.valueOf(1)
			String filePath=HomeSiteNewsConstant.homeSiteNewsHead+homeSiteNews.getId()+"_"+String.valueOf(1)+".html";
			htmlCreateObj.setCreateFileParament(RequestUtils.getRequestHost(request)+HomeSiteNewsConstant.homeSiteNewsPath+homeSiteNews.getId(), "html/news/", filePath);
			htmlCreateObj.createHtmlFile(request, response);
	        //设置静态文件路径
			homeSiteNews.setFilePath(filePath);
			this.homeSiteNewsServImpl.update(homeSiteNews);
		}
		if(contentslist!=null&&!contentslist.isEmpty()&&contentslist.size()>0){
			for(int i=0;i<contentslist.size();i++){
				if(i==0){
			        //生成静态页面
					String filePath=HomeSiteNewsConstant.homeSiteNewsHead+homeSiteNews.getId()+"_"+String.valueOf(i+1)+".html";
					htmlCreateObj.setCreateFileParament(RequestUtils.getRequestHost(request)+HomeSiteNewsConstant.homeSiteNewsPath+homeSiteNews.getId()+"&content="+i, "html/news/", filePath);
					htmlCreateObj.createHtmlFile(request, response);
			        //设置静态文件路径
					homeSiteNews.setFilePath(filePath);
					this.homeSiteNewsServImpl.update(homeSiteNews);
				}else 
				{
			        //生成静态页面
					String filePath=HomeSiteNewsConstant.homeSiteNewsHead+homeSiteNews.getId()+"_"+String.valueOf(i+1)+".html";
					htmlCreateObj.setCreateFileParament(RequestUtils.getRequestHost(request)+HomeSiteNewsConstant.homeSiteNewsPath+homeSiteNews.getId()+"&content="+i, "html/news/", filePath);
					htmlCreateObj.createHtmlFile(request, response);
				}
				
				
			}
			
		}	
		
		request.setAttribute("add", "<script>alert('添加成功！！！');</script>");
		}
		
		//		资讯类别
		BmClassType bmClassType=this.bmClassTypeServImpl.getBmClassTypeByClassTypeName(HomeSiteNewsConstant.zxClassTypeName);
		int bmClassTypeId=0;
		if(bmClassType!=null&&bmClassType.getId()!=null&&bmClassType.getId()!=0){
			
			 bmClassTypeId=bmClassType.getId();

		}
		//		获得老的二级分类和三级分类
		this.loadOldNewsClass(request,homeSiteNews,bmClassTypeId);
		
//		StringBuffer buff=new StringBuffer("/admin/homeSite/homeSiteNews.do?action=toadd");
		
		return mapping.findForward("countinueAdd");
		
	}
	
	
	/********
	 * 生成股票推荐栏目的xml文件数据
	 * @param homeSiteNews
	 */
	private void createCommentStock(HttpServletRequest request,
			HttpServletResponse response,HomeSiteNews homeSiteNews) {
		
		try {
			
			if(homeSiteNews.getHomeSiteNewsClass()!=null){
				
				//股票推荐
				 if(homeSiteNews.getHomeSiteNewsClass().getNames().equals(HomeSiteNewsConstant.COMMENT_STOCK)){
					   //获取前几个新闻
					   List<HomeSiteNews> list=homeSiteNewsServImpl.getCommendHomeSiteNewsListByClassId(homeSiteNews.getHomeSiteNewsClass().getId(), HomeSiteNewsConstant.COMMENT_STOCK_SIZE);
					   if(list!=null  && !list.isEmpty()){
						     //写入到excel文件中.
			                 //String filePath=RequestUtils.getRequestHost(request);
			                 String filePath=this.getServlet().getServletContext().getRealPath("/");
			             
			                 filePath = filePath+HomeSiteNewsConstant.COMMENT_STOCK_FILE_PATH;
			                 File fdir = new File(filePath);
			                 if(!fdir.exists()){//检查是否存在
			                	 fdir.mkdir();
			                 }
			                 
			                
			                 File file = new File(filePath+"info.xml");
			                
			                	 // 新建student.xml文件并新增内容
			                     Document _document = DocumentHelper.createDocument();
			                     Element _root = _document.addElement("NewInfo");
			                     
			                     for(int j=0;j<list.size();j++){
			                    	 HomeSiteNews curHomeSiteNews=(HomeSiteNews)list.get(j);
			                    	 Element _comment = _root.addElement("comment");
			                    	 Element _title = _comment.addElement("Title");
			                    	 _title.setText(curHomeSiteNews.getTitle());
			                    	 
			                    	 Element _content = _comment.addElement("Content");
			                    	 _content.setText("/html/news/"+curHomeSiteNews.getFilePath());
			                    	 
			                    	 Element _date = _comment.addElement("Date");
			                    	 _date.setText(DateUtil.format(curHomeSiteNews.getCreateDatetime(), "yyyy-MM-dd"));
			                    	 
			                    	 
//			                    	 _comment.addAttribute("Title", curHomeSiteNews.getTitle());
//			                    	 _comment.addAttribute("Content", "/html/news/"+curHomeSiteNews.getFilePath());
//			                    	 _comment.addAttribute("Date", DateUtil.format(curHomeSiteNews.getCreateDatetime(), "yyyy-MM-dd"));
			                     }

			                     XMLWriter writer = null;// 声明写XML的对象
			                     OutputFormat format = OutputFormat.createPrettyPrint();
			                     format.setEncoding("GBK");// 设置XML文件的编码格式
			                     writer = new XMLWriter(new FileWriter(file), format);
			                     writer.write(_document);
			                     writer.close();
			                     
						      
					   }
					   
				 }
				
			}
			
		} catch (BusinessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}


	/****
	 * 获得老的二级分类和三级分类
	 * @param request
	 * @param homeSiteNews
	 * @param bmClassTypeId
	 */
	private void loadOldNewsClass(HttpServletRequest request, HomeSiteNews homeSiteNews, int bmClassTypeId)
	{
        Long newsClassId = homeSiteNews.getHomeSiteNewsClass().getId();
	    
		//获取
		try {
			//获得分类
			HomeSiteNewsClass  homeSiteNewsClass =this.getHomeSiteNewsClassServ().loadNewsClassById(newsClassId);
			String seq=homeSiteNewsClass.getSeq();
			
			//一级分类id
			String firstNewsClassId="";
			//二级分类id
			String secondNewsClassId="";
			//三级分类id
			String thirdNewsClassId="";
			
			Map<Long, String> secondNewsClassMap = new HashMap<Long, String>();
			
			Map<Long, String> thirdNewsClassMap = new HashMap<Long, String>();
			
			if(seq!=null  && !seq.equals(""))
			{
				if(seq.indexOf(",")!=-1)
				{
				   String[] param=seq.split(",");
				   //获得第一级分类列表
				   if(param.length>=3)
				   {
					   firstNewsClassId = param[2];
				   }
				   
				   //获得第二级的分类列表
				   if(param.length>=4)
				   {
					     secondNewsClassId = param[3];
				   }
				   
				   //获得第三级的分类列表
				   if(param.length>=5)
				   {
					   thirdNewsClassId = param[4];
				   }
				}
			}
			
		
		//通过一级分类id,获取二级分类列表
			if(firstNewsClassId!=null  && !firstNewsClassId.equals(""))
			{
				request.setAttribute("homeSiteNewsClassBid", firstNewsClassId);
				secondNewsClassMap = this.secondNewsClassListByFirstClassId(Long.valueOf(firstNewsClassId));
			}
			
			//通过二级分类id,获取三级分类的广告列表
			if(secondNewsClassId!=null   && !secondNewsClassId.equals(""))
			{
				request.setAttribute("homeSiteNewsClassSid", secondNewsClassId);
				thirdNewsClassMap = this.secondNewsClassListByFirstClassId(Long.valueOf(secondNewsClassId));
			}
			
			//如果三级分类存在,则设置三级分类
			if(thirdNewsClassId!=null  && !thirdNewsClassId.equals(""))
			{
				request.setAttribute("homeSiteNewsClassThid", thirdNewsClassId);
			}
			
			
			request.setAttribute("mapsecond",secondNewsClassMap );
			
			request.setAttribute("mapthird",thirdNewsClassMap);
			
			
		} catch (BusinessException e) {
			e.printStackTrace();
		}
	}

    /*****
     * 通过父类id,获取,新闻资讯的列表
     * @param long1
     * @return
     */
	@SuppressWarnings("unchecked")
	private Map<Long, String> secondNewsClassListByFirstClassId(Long parId) 
	{
		  try {
			  return this.getHomeSiteNewsClassServ().loadSecondHomeSiteNewsClassByNewsType(parId);
			} catch (BusinessException e) {
				e.printStackTrace();
			}
			return new HashMap();
	}


	/**
	 * 
	 * 设置资讯分类
	 * 
	 * */
	
	public void addHomeSiteNewsClass(HomeSiteNewsForm homeSiteNewsForm,HomeSiteNews homeSiteNews)throws Exception{
		
		HomeSiteNewsClass homeSiteNewsClass=null;
		//1级分类
		if(homeSiteNewsForm.getHomeSiteNewsClassBid()!=-1&&(homeSiteNewsForm.getHomeSiteNewsClassSid()==-1)){
			
			 homeSiteNewsClass=this.homeSiteNewsClassServ.loadNewsClassById(homeSiteNewsForm.getHomeSiteNewsClassBid());
			
			if(homeSiteNewsClass!=null){
				homeSiteNews.setHomeSiteNewsClass(homeSiteNewsClass);
			}
		}
            //2级分类
		else if(homeSiteNewsForm.getHomeSiteNewsClassBid()!=-1&&homeSiteNewsForm.getHomeSiteNewsClassSid()!=-1&&homeSiteNewsForm.getHomeSiteNewsClassThid()==-1){
			
			 homeSiteNewsClass=this.homeSiteNewsClassServ.loadNewsClassById(homeSiteNewsForm.getHomeSiteNewsClassSid());
			
			if(homeSiteNewsClass!=null){
				homeSiteNews.setHomeSiteNewsClass(homeSiteNewsClass);
			}
		}
		//3级分类
		else if(homeSiteNewsForm.getHomeSiteNewsClassBid()!=-1&&homeSiteNewsForm.getHomeSiteNewsClassSid()!=-1&&homeSiteNewsForm.getHomeSiteNewsClassThid()!=-1){
			 homeSiteNewsClass=this.homeSiteNewsClassServ.loadNewsClassById(homeSiteNewsForm.getHomeSiteNewsClassThid());
			
			if(homeSiteNewsClass!=null){
				homeSiteNews.setHomeSiteNewsClass(homeSiteNewsClass);
			}		
		}
		
	}
	
	
	
      //上传图片
	public void addImg(HomeSiteNewsForm homeSiteNewsForm,HomeSiteNews homeSiteNews,HttpServletRequest request,String picstr){
		if(homeSiteNewsForm.getImgstr()!=null&&!homeSiteNewsForm.getImgstr().getFileName().trim().equals(""))
		{
		String uploadPath="fileUpload"+File.separator+"news"+File.separator;
		imgUpload.setMyFiles(homeSiteNewsForm.getImgstr());
		imgUpload.setPath(uploadPath);
		imgUpload.setPassFileExtenSionName(HomeSiteNewsConstant.DEFAULT_EXT_NAME);
		String fileName =null;
		if(picstr==null)
		{
			 fileName = imgUpload.uploadFile(request, imgUpload.loadUploadFileName());
		}
		else {
			fileName=imgUpload.uploadFile(request, picstr);	
		}
		String imgPath = fileName;
		homeSiteNews.setImg(imgPath);
		}
		
	}
	
	
	
	/**
	 * 
	 * 加载资讯分类
	 * */
	public void loadhomeSiteNewsClassList(HttpServletRequest request,HomeSiteNews homeSiteNews,int bmClassTypeId ){
		//加载资讯分类
		HomeSiteNewsClass homeSiteNewsClass=null;
		if(homeSiteNews!=null){
			homeSiteNewsClass=homeSiteNews.getHomeSiteNewsClass();
			if(homeSiteNewsClass!=null&&homeSiteNewsClass.getId()!=0){
				
				try {
					homeSiteNewsClass=this.homeSiteNewsClassServ.loadNewsClassById(Long.valueOf(homeSiteNewsClass.getId()));
				} catch (BusinessException e) {
					e.printStackTrace();
				}
				
				String seqStr=homeSiteNewsClass.getSeq();
				Integer layer=0;
				
				if(seqStr!=null&&seqStr.indexOf(",")!=-1){
					
					String[] str=seqStr.split(",");
					if(str!=null)
					{
						layer=str.length-2;
					}
					//第一级
					if(layer.intValue() == 0 ||layer.intValue()==1)
					{

						if(seqStr!=null&&!seqStr.equals("")&&seqStr.indexOf(",")!=-1)
						{
							String seq[]=seqStr.split(",");
							int len=seq.length;
							request.setAttribute("homeSiteNewsClassBid", Long.valueOf(seq[2]));
							
							if(len==2){
								request.setAttribute("homeSiteNewsClassSid", Long.valueOf("-1"));
							}
							else if(len>2){
								request.setAttribute("homeSiteNewsClassSid", Long.valueOf("-1"));
							}
						}
						request.setAttribute("homeSiteNewsClassThid", Long.valueOf("-1"));
						
					}
					//
					if(layer.intValue()==2)
					{
						if(seqStr!=null&&!seqStr.equals("")&&seqStr.indexOf(",")!=-1){
							String seq[]=seqStr.split(",");
							int len=seq.length;
							if(len>=2&&bmClassTypeId!=0){
								
								List mapsecond;
								try {
									mapsecond = this.homeSiteNewsClassServ.loadNewsClassList(Long.valueOf(Long.valueOf(seq[2])),Integer.valueOf(bmClassTypeId));
									request.setAttribute("mapsecond", mapsecond);
								} catch (NumberFormatException e) {
									e.printStackTrace();
								} catch (BusinessException e) {
									e.printStackTrace();
								}
								
								request.setAttribute("homeSiteNewsClassBid", Long.valueOf(seq[2]));
								
								Long secondId=Long.valueOf(seq[3]);
								
								request.setAttribute("homeSiteNewsClassSid", secondId);
								request.setAttribute("homeSiteNewsClassThid", Long.valueOf("-1"));
							}

						}
						
					}
					
					//
					
					if(layer.intValue()>2){
					if(seqStr!=null&&!seqStr.equals("")&&seqStr.indexOf(",")!=-1){
						String seq[]=seqStr.split(",");
						int len=seq.length;
						if(len>=3&&bmClassTypeId!=0){
							
							List mapsecond;
							try {
								mapsecond = this.homeSiteNewsClassServ.loadNewsClassList(Long.valueOf(Long.valueOf(seq[2])),Integer.valueOf(bmClassTypeId));
								request.setAttribute("mapsecond", mapsecond);
							} catch (NumberFormatException e) {
								e.printStackTrace();
							} catch (BusinessException e) {
								e.printStackTrace();
							}
							
							request.setAttribute("homeSiteNewsClassBid", Long.valueOf(seq[2]));
							
							Long secondId=Long.valueOf(seq[3]);
							List mapthird;
							try {
								mapthird = this.homeSiteNewsClassServ.loadNewsClassList(Long.valueOf(secondId),Integer.valueOf(bmClassTypeId));
								request.setAttribute("mapthird", mapthird);
							} catch (BusinessException e) {
								e.printStackTrace();
							}
							
							request.setAttribute("homeSiteNewsClassSid", secondId);
							request.setAttribute("homeSiteNewsClassThid", Long.valueOf(seq[4]));
						}

					}
					
				}

			}	
		}
	}
	}
	
	
	
	
	/**
	 * 去修改
	 * */
	public ActionForward toupdate(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		HomeSiteNewsForm homeSiteNewsForm=(HomeSiteNewsForm)form;
		HomeSiteNews homeSiteNews=null;

		//加载城市列表
		String idstr=request.getParameter("id");
		if(idstr!=null&&!idstr.equals(""))
		{
			homeSiteNews=this.homeSiteNewsServImpl.getHomeSiteNewsById(Long.valueOf(idstr));
		}
		//资讯类别
		BmClassType bmClassType=this.bmClassTypeServImpl.getBmClassTypeByClassTypeName(HomeSiteNewsConstant.zxClassTypeName);
		request.setAttribute("bmClassTypeId", bmClassType.getId());
		
		this.loadFirstLayerList(request, bmClassType.getId());
		
		//加载二级三级分类列表
		this.loadOldNewsClass(request, homeSiteNews, bmClassType.getId());
		//属性拷贝
		BeanUtils.copyProperties(homeSiteNewsForm, homeSiteNews);
		request.setAttribute("homeSiteNewsForm", homeSiteNewsForm);
		
		//图片
		String imgsStr=homeSiteNews.getImg();
		if(imgsStr!=null){
			request.setAttribute("imgsStr", imgsStr);
			
		}
		
		//获取参数
		
		String page=request.getParameter("page")!=null? request.getParameter("page"):"";
		String pageSize=request.getParameter("pageSize")!=null? request.getParameter("pageSize"):"";
		String bktitle=request.getParameter("bktitle")!=null? request.getParameter("bktitle"):"";
		request.setAttribute("page", page);
		request.setAttribute("pageSize", pageSize);
		request.setAttribute("bktitle", bktitle);
		
		return mapping.findForward("toupdate");
		
	}
	
	
	
	
	/**
	 * 修改
	 * */
	public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		HomeSiteNewsForm homeSiteNewsForm=(HomeSiteNewsForm)form;
		if(null!=homeSiteNewsForm.getImgstr()&& !"".equals(homeSiteNewsForm.getImgstr())
				&& null!=homeSiteNewsForm.getImgstr().getFileName()
				&& homeSiteNewsForm.getImgstr().getFileSize()>1024*20){
			request.setAttribute("update", "<script>alert('修改失败！！！，图片不可以大于20k');</script>");
		}
		else{
		HomeSiteNews homeSiteNews=null;
		homeSiteNews=this.homeSiteNewsServImpl.getHomeSiteNewsById(homeSiteNewsForm.getId());
		
		homeSiteNews.setTitle(homeSiteNewsForm.getTitle());
		homeSiteNews.setContent(homeSiteNewsForm.getContent());
		homeSiteNews.setKeyword(homeSiteNewsForm.getKeyword());
		
		homeSiteNews.setWriter(homeSiteNewsForm.getWriter());
		
		homeSiteNews.setSource(homeSiteNewsForm.getSource());
		
		homeSiteNews.setIsDel(homeSiteNewsForm.getIsDel());
		
		homeSiteNews.setIscommend(homeSiteNewsForm.getIscommend());
		homeSiteNews.setToptitle(homeSiteNewsForm.getToptitle());
		homeSiteNews.setAudite(homeSiteNewsForm.getAudite());
		homeSiteNews.setIsImg(homeSiteNewsForm.getIsImg());
		homeSiteNews.setIsImgSmallTitle(homeSiteNewsForm.getIsImgSmallTitle());
		homeSiteNews.setImgTitle(homeSiteNewsForm.getImgTitle());
		
		homeSiteNews.setIsOutLink(homeSiteNewsForm.getIsOutLink());
		
		homeSiteNews.setOutLink(homeSiteNewsForm.getOutLink());
		
		//设置热点新闻
		homeSiteNews.setHotspot(homeSiteNewsForm.getHotspot());
		
		//设置资讯分类
		this.addHomeSiteNewsClass(homeSiteNewsForm, homeSiteNews);

	    //上传图片
		String picstr=null;
		picstr=homeSiteNews.getImg();
		if(picstr!=null&&picstr.indexOf(".")!=-1){
			
			picstr=picstr.substring(0, picstr.indexOf("."));
		}
		this.addImg(homeSiteNewsForm, homeSiteNews, request, picstr);
		
		//修改时间
		java.util.Date date=new java.util.Date();
		homeSiteNews.setUpdateDatetime(date);
		
		//修改
		this.homeSiteNewsServImpl.update(homeSiteNews);
		request.setAttribute("update", "<script>alert('修改成功！！！');</script>");
		
    	String spliter="<div style=\"page-break-after: always\"><span style=\"display: none\">&nbsp;</span></div>";
    	List<String> contentslist=new ArrayList<String>();
		String con=homeSiteNews.getContent();
		if(con!=null&&!con.equals("")&&con.indexOf(spliter)!=-1){
			String[] contents=con.split(spliter);
			if(contents!=null&&contents.length>0){
				for(int i=0;i<contents.length;i++){
					if(contents[i]!=null&&!contents.equals("")){
						contentslist.add(contents[i]);
					}
				}
			}
		}else 
		{
//	        //生成静态页面
			String filePath=HomeSiteNewsConstant.homeSiteNewsHead+homeSiteNews.getId()+"_"+String.valueOf(1)+".html";
			htmlCreateObj.setCreateFileParament(RequestUtils.getRequestHost(request)+HomeSiteNewsConstant.homeSiteNewsPath+homeSiteNews.getId(), "html/news/", filePath);
			htmlCreateObj.createHtmlFile(request, response);
	        //设置静态文件路径
			homeSiteNews.setFilePath(filePath);
			this.homeSiteNewsServImpl.update(homeSiteNews);
		}
		if(contentslist!=null&&!contentslist.isEmpty()&&contentslist.size()>0){
			for(int i=0;i<contentslist.size();i++){
				if(i==0){
			        //生成静态页面
					String filePath=HomeSiteNewsConstant.homeSiteNewsHead+homeSiteNews.getId()+"_"+String.valueOf(i+1)+".html";
					htmlCreateObj.setCreateFileParament(RequestUtils.getRequestHost(request)+HomeSiteNewsConstant.homeSiteNewsPath+homeSiteNews.getId()+"&content="+i, "html/news/", filePath);
					htmlCreateObj.createHtmlFile(request, response);
			        //设置静态文件路径
					homeSiteNews.setFilePath(filePath);
					this.homeSiteNewsServImpl.update(homeSiteNews);
				}else 
				{
			        //生成静态页面
					String filePath=HomeSiteNewsConstant.homeSiteNewsHead+homeSiteNews.getId()+"_"+String.valueOf(i+1)+".html";
					htmlCreateObj.setCreateFileParament(RequestUtils.getRequestHost(request)+HomeSiteNewsConstant.homeSiteNewsPath+homeSiteNews.getId()+"&content="+i, "html/news/", filePath);
					htmlCreateObj.createHtmlFile(request, response);
				}
				
				
			}
			
		  }	
		}
		StringBuffer buff=new StringBuffer("/admin/homeSite/homeSiteNews.do?action=list");
		String page=request.getParameter("page");
		if(page!=null&&!page.equals("")){
		buff.append("&page=").append(page);
		}
	String pageSize=request.getParameter("pageSize");
	if(pageSize!=null&&!pageSize.equals("")){
		buff.append("&pageSize=").append(pageSize);
	}
	
	String isDel=request.getParameter("bkisDel");
	if(isDel!=null){
		
		buff.append("&isDel=").append(isDel);
	}
	
	String homeSiteNewsClassid=request.getParameter("bkhomeSiteNewsClassid");
	if(homeSiteNewsClassid!=null){
		
		buff.append("&homeSiteNewsClassid=").append(homeSiteNewsClassid);
	}
	String bkcontent=request.getParameter("bkcontent");
	if(bkcontent!=null){
		
		buff.append("&bkcontent=").append(bkcontent);
	}
	String bkcreateDatetimeStr=request.getParameter("bkcreateDatetimeStr");
	if(bkcreateDatetimeStr!=null){
		
		buff.append("&bkcreateDatetimeStr=").append(bkcreateDatetimeStr);
	}
	
	
	
	String title=request.getParameter("bktitle");
	if(title!=null){
		
		buff.append("&title=").append(title);
	}
	
	return new ActionForward(buff.toString());
		
	}
	
	/**
	 * 删除
	 * */
	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
	      String[] idStr = request.getParameterValues("checkboxparam");
	      
	      long[] intId = StringUtil.changeStrArrayToLongArray(idStr);
	      if(intId!=null){
	    	  for(int i=0;i<intId.length;i++){
	    			  
	    			  this.homeSiteNewsServImpl.delete(intId[i]);
	    			  request.setAttribute("del", "<script>alert('删除成功！！！');</script>");
	    			  
	    		  } 
	    	  }
		return mapping.findForward("delete");
	}
	
	
	
	
	/**
	 * 查询
	 * */
	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		String topage="list";
		
//		获取所有的资讯分类的第一级分类
		BmClassType bmClassType=this.bmClassTypeServImpl.getBmClassTypeByClassTypeName(HomeSiteNewsConstant.zxClassTypeName);
		
//		获取父分类下面的所有一级分类
		this.loadFirstLayerList(request,bmClassType.getId());
		request.setAttribute("bmClassTypeId", bmClassType.getId());

		this.loadHomeSiteNewsList(request);
		
		return mapping.findForward(topage);
		
	}
	
	
	@SuppressWarnings("unchecked")
	private void loadHomeSiteNewsList(HttpServletRequest request){
		
		
	      ProductsParamentObj parament=new ProductsParamentObj();
	      
	      String[] ret=parament.dealWithParaments(request,
	    		  new String[]{PageContent.page,PageContent.pageSize,PageContent.doit,
	    		  PageContent.backPage,PageContent.nextPage,"keyword","qyId","iscommend","personalId","title",
	    		  "provId","cityId","diquId","homeSiteNewsClassid","isDel","content","createDatetimeStr"});

	      
	      int page=PageContent.pageValue;
	      
	      int pageSize=PageContent.pageSizeValue;
	      
	      
	      
	      String doit="";
	      
	      //页码
	      if(ret[0]!=null)
	      {
	          page=Integer.parseInt(ret[0]);
	      }
	      
	      
	      //显示数
	      if(ret[1]!=null)
	      {
	          pageSize=Integer.parseInt(ret[1]);
	      }  
	      
	      
	      //跳转
	      if(ret[2]!=null)
	      {
	          doit=ret[2];
	      }
	      
	      //上一页
	      if(doit!=null&&doit.equals(PageContent.backPage)&&ret[3]!=null&&!ret[3].equals(""))
	      {
	          page=Integer.parseInt(ret[3]);
	      }
	      
	      //下一页
	      if(doit!=null&&doit.equals(PageContent.nextPage)&&ret[4]!=null&&!ret[4].equals(""))
	      {
	          page=Integer.parseInt(ret[4]);
	      }
	      
	      String keyword="";
	      //页码
	      if(ret[5]!=null)
	      {
	    	  try {
				keyword=new String(ret[5].trim().getBytes("ISO-8859-1"),"UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
	      }
	      
	      Integer qyId=null;
	      
	      if(ret[6]!=null)
	      {
	    	  qyId=Integer.valueOf(ret[6]);
	      }
	      
	      
	      Integer iscommend=null;
	      if(ret[7]!=null)
	      {
	    	  iscommend=Integer.valueOf(ret[7]);
	      }
	      
	      long personalId=0;
	      
	      if(ret[8]!=null)
	      {
	    	  personalId=Long.valueOf(ret[8]);
	      }
	      
	      
	      String title="";
	      //页码
	      if(ret[9]!=null)
	      {
	    	  try {
				title=new String(ret[9].trim().getBytes("ISO-8859-1"),"UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
	      }
	      
	      Integer provId=null;
	      if(ret[10]!=null){
	    	  provId=Integer.valueOf(ret[10]);
	      }
	      Integer cityId=null;
	      if(ret[11]!=null){
	    	  cityId=Integer.valueOf(ret[11]);
	      }
	      Integer diquId=null;
	      if(ret[12]!=null){
	    	  diquId=Integer.valueOf(ret[12]);
	      }
	      
	      long homeSiteNewsClassid=0;
	      if(ret[13]!=null){
	    	  homeSiteNewsClassid=Long.valueOf(ret[13]);
	      }
	      
	      short isDel=-2;
	      if(ret[14]!=null){
	    	  isDel=Short.valueOf(ret[14]);
	      }
	      String  content="";
	      if(ret[15]!=null){
	    	  try {
				content=new String(ret[15].trim().getBytes("ISO-8859-1"),"UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
	      }
	      String createDatetimeStr="";
	      if(ret[16]!=null){
	    	  createDatetimeStr=ret[16];
	      }
	      
	      HomeSiteNewsFormExtendBean homeSiteNewsFormExtendBean=new HomeSiteNewsFormExtendBean();
	      
	      homeSiteNewsFormExtendBean.setPage(page);
	      homeSiteNewsFormExtendBean.setPageSize(pageSize);
	      homeSiteNewsFormExtendBean.setKeyword(keyword);
	      homeSiteNewsFormExtendBean.setQyId(qyId);
	      homeSiteNewsFormExtendBean.setIscommend(iscommend);
	      homeSiteNewsFormExtendBean.setPersonalId(personalId);
	      homeSiteNewsFormExtendBean.setTitle(title);
	      homeSiteNewsFormExtendBean.setProvId(provId);
	      homeSiteNewsFormExtendBean.setCityId(cityId);
	      homeSiteNewsFormExtendBean.setDiquId(diquId);
	      homeSiteNewsFormExtendBean.setHomeSiteNewsClassid(homeSiteNewsClassid);
	      homeSiteNewsFormExtendBean.setIsDel(isDel);
	      homeSiteNewsFormExtendBean.setContent(content);
	      homeSiteNewsFormExtendBean.setCreateDatetimeStr(createDatetimeStr);
	      
	      
	      
	      try {
	    	  PageResultSet pageResultSet=this.homeSiteNewsServImpl.loadHomeSiteNewsByExtendBean(homeSiteNewsFormExtendBean);
				  if(pageResultSet!=null)
					{
					  	// 设置上一页
						request.setAttribute(PageContent.backPage,pageResultSet.getBackPage());
						//下一页
						request.setAttribute(PageContent.nextPage,pageResultSet.getNextPage());
						//当前页
						request.setAttribute(PageContent.curPage,pageResultSet.getCurPage());
						
						//总共页数
						request.setAttribute(PageContent.allPage,pageResultSet.getAllPage());
						//大小
						request.setAttribute(PageContent.pageSize,pageResultSet.getPageSize());
						
						request.setAttribute(PageContent.recordNum, pageResultSet.getRecordNum());
						
						
						request.setAttribute("title",title!=null? title:"");
						request.setAttribute("content",content!=null? content:"");
						request.setAttribute("createDatetimeStr",createDatetimeStr!=null? createDatetimeStr:"");
						request.setAttribute("homeSiteNewsClassid",homeSiteNewsClassid);
						request.setAttribute("page",page);
						
						
						if(homeSiteNewsClassid!=0){
							
							HomeSiteNewsClass homeSiteNewsClass=this.homeSiteNewsClassServ.loadNewsClassById(homeSiteNewsClassid);
							if(homeSiteNewsClass!=null){
								request.setAttribute("homeSiteNewsClassStr", homeSiteNewsClass);
							}
							
						}
						

						//结果集
						List result=null;
						List exresult=new ArrayList();
						if(pageResultSet!=null)
						{
							result =pageResultSet.getResult();
							if(result!=null){
								for(int i=0;i<result.size();i++){
									HomeSiteNews homeSiteNews=(HomeSiteNews) result.get(i);
									
									HomeSiteNewsExtendBean homeSiteNewsExtendBean=new HomeSiteNewsExtendBean();
									try {
										BeanUtils.copyProperties(homeSiteNewsExtendBean, homeSiteNews);
									} catch (IllegalAccessException e) {
										e.printStackTrace();
									} catch (InvocationTargetException e) {
										e.printStackTrace();
									}
									
									if(homeSiteNewsExtendBean!=null){
										
										//加载资讯分类
										HomeSiteNewsClass homeSiteNewsClass=homeSiteNewsExtendBean.getHomeSiteNewsClass();
										if(homeSiteNewsClass!=null&&homeSiteNewsClass.getId()!=0){
											
											long thid=homeSiteNewsClass.getId();
											
											HomeSiteNewsClass homeSiteNewsThClass=this.homeSiteNewsClassServ.loadNewsClassById(thid);
											if(homeSiteNewsThClass!=null){
												
												homeSiteNewsExtendBean.setHomeSiteNewsThClass(homeSiteNewsThClass);
												long sid=homeSiteNewsThClass.getParenId();
												if(sid!=0&&sid!=-1){
													
													HomeSiteNewsClass homeSiteNewsSClass=this.homeSiteNewsClassServ.loadNewsClassById(sid);
													if(homeSiteNewsSClass!=null){
														
														homeSiteNewsExtendBean.setHomeSiteNewsSClass(homeSiteNewsSClass);
														long bid=homeSiteNewsSClass.getParenId();
														if(bid!=0&&bid!=-1){
															HomeSiteNewsClass homeSiteNewsBClass=this.homeSiteNewsClassServ.loadNewsClassById(sid);
															if(homeSiteNewsBClass!=null){
																homeSiteNewsExtendBean.setHomeSiteNewsBClass(homeSiteNewsBClass);
															}
															
														}
													}
													
												}
												
											}
										}
										exresult.add(homeSiteNewsExtendBean);
									}
									
								}
								
							}
							
						}
						else
						{
							exresult=new ArrayList();
						}
						
						request.setAttribute("HomeSiteNewsList", exresult);
						
					}
				  
				  
			} catch (BusinessException e) {
				e.printStackTrace();
			}
	}
	
	
    /*****
     * 批量处理 
     */
    public ActionForward dealWithAll(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) {
        
        //获取处理操作
        String doit=request.getParameter("doit");      
        //      返回参数
        String page=request.getParameter("page");
        
        //返回参数 
        String pageSize=request.getParameter("pageSize");

        String[] checkboxparam=request.getParameterValues("checkboxparam");
        
        long[] ids=null;
        
        if(checkboxparam!=null&&checkboxparam.length>0)
        {
            ids=new long[checkboxparam.length];
            
            for(int j=0;j<checkboxparam.length;j++)
            {
               ids[j]=Long.valueOf(checkboxparam[j]);
            }
        }
        
        
        if(doit==null)
        {
            doit="";
        }
        
        //推荐
        if(doit.equals("tuijian"))
        {
            manyDealWithProducts(request,ids,"tuijian",new Integer("1"));
            request.setAttribute("succe", "<script>alert('推荐成功');</script>");
        }
        //推荐
        if(doit.equals("deltuijian"))
        {
        	manyDealWithProducts(request,ids,"tuijian",new Integer("0"));
        	request.setAttribute("succe", "<script>alert('取消推荐成功');</script>");
        }
        //审核
        if(doit.equals("audite"))
        {
        	short audite=1;
        	manyDealWithProducts(request,ids,"audite",audite);
        	request.setAttribute("succe", "<script>alert('审核成功');</script>");
        }
        //取消审核
        if(doit.equals("delaudite"))
        {
        	short audite=0;
        	manyDealWithProducts(request,ids,"audite",audite);
        	request.setAttribute("succe", "<script>alert('取消审核成功');</script>");
        }
        //放入回收站
        if(doit.equals("isDel"))
        {
        	manyDealWithProducts(request,ids,"isDel",new Short("1"));
        	request.setAttribute("succe", "<script>alert('成功放入回收站');</script>");
        }
        //恢复
        if(doit.equals("delisDel"))
        {
        	manyDealWithProducts(request,ids,"isDel",new Short("0"));
        	request.setAttribute("succe", "<script>alert('恢复成功');</script>");
        }
        //头条
        if(doit.equals("toptitle"))
        {
        	manyDealWithProducts(request,ids,"toptitle",new Short("1"));
        	request.setAttribute("succe", "<script>alert('头条成功');</script>");
        }
        //取消头条
        if(doit.equals("deltoptitle"))
        {
        	manyDealWithProducts(request,ids,"toptitle",new Short("0"));
        	request.setAttribute("succe", "<script>alert('取消头条成功');</script>");
        }
        
        //热点
        if(doit.equals("hotspot"))
        {
        	manyDealWithProducts(request,ids,"hotspot",new Short("1"));
        	request.setAttribute("succe", "<script>alert('热点设置成功');</script>");
        }
        //取消热点
        if(doit.equals("delhotspot"))
        {
        	manyDealWithProducts(request,ids,"hotspot",new Short("0"));
        	request.setAttribute("succe", "<script>alert('取消热点成功');</script>");
        }
        
        //彻底删除
        if(doit.equals("readlyisDel"))
        {
        	manyDealWithProducts(request,ids,"readlyisDel",new Short("1"));
        	request.setAttribute("succe", "<script>alert('彻底删除成功');</script>");
        }
        StringBuffer link=new StringBuffer("/admin/homeSite/homeSiteNews.do?action=list");
        //是否推荐
        if(page!=null&&!page.trim().equals(""))
        {
        	link.append("&page=").append(page);
        }
        //名称
        if(pageSize!=null&&!pageSize.trim().equals(""))
        {
        	link.append("&pageSize=").append(pageSize);
        }
        
        
    	String provId=request.getParameter("bkprovId");
    	if(provId!=null){
    		
    		link.append("&provId=").append(provId);
    	}
    	String cityId=request.getParameter("bkcityId");
    	if(cityId!=null){
    		
    		link.append("&cityId=").append(cityId);
    	}
    	String isDel=request.getParameter("bkisDel");
    	if(isDel!=null){
    		
    		link.append("&isDel=").append(isDel);
    	}else{
    		link.append("&isDel=").append("0");
    	}
    	
    	String homeSiteNewsClassid=request.getParameter("bkhomeSiteNewsClassid");
    	if(homeSiteNewsClassid!=null){
    		
    		link.append("&homeSiteNewsClassid=").append(homeSiteNewsClassid);
    	}
    	
    	
    	
    	String title=request.getParameter("bktitle");
    	if(title!=null){
    		
    		link.append("&title=").append(title);
    	}
    	String content=request.getParameter("bkcontent");
    	if(content!=null){
    		
    		link.append("&content=").append(content);
    	}
    	String createDatetimeStr=request.getParameter("bkcreateDatetimeStr");
    	if(createDatetimeStr!=null){
    		
    		link.append("&createDatetimeStr=").append(createDatetimeStr);
    	}
            
           
          //加载经销商后台参数,跳转到经销商列表页面  
          return new ActionForward(link.toString());
    }
    
   
    
    
    
    /******
     * 处理批量操作商品  比如批量推荐
     * 订单id数组,处理的列,值
     */
    public void  manyDealWithProducts(HttpServletRequest request,long[] ids,String column,Object value)
    {
        try {
        	if(column!=null&&!column.equals("readlyisDel"))
        	{
        		 if(ids!=null&&ids.length>0)
                 {
                     for(int j=0;j<ids.length;j++){
                    	 HomeSiteNews homeSiteNews=this.homeSiteNewsServImpl.getHomeSiteNewsById(ids[j]);

                         //推荐
                         if(column.equals("tuijian"))
                         {
                        	 homeSiteNews.setIscommend((Integer)value);
                         }
                         //审核
                         if(column.equals("audite"))
                         {
                        	 homeSiteNews.setAudite((Short)value);
                         }
                         //头条
                         if(column.equals("toptitle"))
                         {
                        	 homeSiteNews.setToptitle((Short)value);
                         }
                         //热点
                         if(column.equals("hotspot"))
                         {
                        	 homeSiteNews.setHotspot((Short)value);
                         }
                         
                         //删除
                         if(column.equals("isDel"))
                         {
                        	 homeSiteNews.setIsDel((Short)value);
                         }
                         
                         //更新产品属性
                         this.homeSiteNewsServImpl.update(homeSiteNews);
                         
                     }
                 }
        	}
        	
        	else
        	{
        		 if(ids!=null&&ids.length>0)
                 {
                     for(int j=0;j<ids.length;j++)
                     {
                  	   String url=request.getSession().getServletContext().getRealPath("/")+"html\\news\\"+"news_"+ids[j]+"_1.html";
                	   File newsFile=new File(url);
                	   if(newsFile.exists()){
                		   newsFile.delete();
                	   }
                         this.homeSiteNewsServImpl.delete(ids[j]);
                     }
                 }
        		
        	}
        	
        } catch (BusinessException e) {
            e.printStackTrace();
        }
    }
    
    
   
    
	@SuppressWarnings("unchecked")
	private void loadList(Integer qyId,Integer provId,Integer cityId,Long cid,HttpServletRequest request,Integer page, Integer pageSize){
		try {
			PageResultSet pageResultSet = null;
				// pageResultSet = this.homeSiteNewsServImpl.loadByClassId(cid, page, pageSize); 
				pageResultSet = this.homeSiteNewsServImpl.loadByProvOrCityId(qyId,provId, cityId,cid, page, pageSize);
			  
			  if(pageResultSet!=null)
			  {
			      //设置上一页
			      request.setAttribute(PageContent.backPage,pageResultSet.getBackPage());
			      //下一页
			      request.setAttribute(PageContent.nextPage,pageResultSet.getNextPage());
			      //当前页
			      request.setAttribute(PageContent.curPage,pageResultSet.getCurPage());
			      
			      //总共页数
			      request.setAttribute(PageContent.allPage,pageResultSet.getAllPage());
			      
			      request.setAttribute(PageContent.page, page);
			      request.setAttribute(PageContent.pageSize, pageResultSet.getPageSize());
			      
			      //总记录数
			      request.setAttribute(PageContent.recordNum,pageResultSet.getRecordNum());
			      
			      //开始行码
			      request.setAttribute(PageContent.startRow,pageResultSet.getStartRow());
			      
			      //结束行码
			      request.setAttribute(PageContent.endRow,pageResultSet.getEndRow());			      			      
			      
			      List<HomeSiteNews> li = pageResultSet.getResult();
			      request.setAttribute("HomeSiteNews_list",li);
			      request.setAttribute("cid", cid);
			      
			  }
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (BusinessException e) {
			e.printStackTrace();
		}
		
	}
    
	

	
	public IHomeSiteNewsServ getHomeSiteNewsServImpl() {
		return homeSiteNewsServImpl;
	}
	public void setHomeSiteNewsServImpl(IHomeSiteNewsServ homeSiteNewsServImpl) {
		this.homeSiteNewsServImpl = homeSiteNewsServImpl;
	}
	
	public IBmClassTypeServ getBmClassTypeServImpl() {
		return bmClassTypeServImpl;
	}

	public void setBmClassTypeServImpl(IBmClassTypeServ bmClassTypeServImpl) {
		this.bmClassTypeServImpl = bmClassTypeServImpl;
	}

	

	public IHomeSiteNewsClassServ getHomeSiteNewsClassServ() {
		return homeSiteNewsClassServ;
	}


	public void setHomeSiteNewsClassServ(
			IHomeSiteNewsClassServ homeSiteNewsClassServ) {
		this.homeSiteNewsClassServ = homeSiteNewsClassServ;
	}


	public FileUplaodAndManageForStorage getImgUpload() {
		return imgUpload;
	}

	public void setImgUpload(FileUplaodAndManageForStorage imgUpload) {
		this.imgUpload = imgUpload;
	}


	
	public HtmlCreateObj getHtmlCreateObj() {
		return htmlCreateObj;
	}

	public void setHtmlCreateObj(HtmlCreateObj htmlCreateObj) {
		this.htmlCreateObj = htmlCreateObj;
	}
	
	

	public IHomeSiteAdBannerDescriptServ getHomeSiteAdBannerDescriptServ() {
		return homeSiteAdBannerDescriptServ;
	}

	public void setHomeSiteAdBannerDescriptServ(
			IHomeSiteAdBannerDescriptServ homeSiteAdBannerDescriptServ) {
		this.homeSiteAdBannerDescriptServ = homeSiteAdBannerDescriptServ;
	}

	public IHomeSiteAdBannerServ getHomeSiteAdBannerServ() {
		return homeSiteAdBannerServ;
	}

	public void setHomeSiteAdBannerServ(IHomeSiteAdBannerServ homeSiteAdBannerServ) {
		this.homeSiteAdBannerServ = homeSiteAdBannerServ;
	}

	


}
